(function($,window,layer,document,undefined) {
    
	//获取html页面的url地址
    var htmlPath = (function(){
        var e=document.scripts,
        n=e[e.length-1],i=n.src,a=i.substring(0,i.lastIndexOf("/")+1);
        return a+'voucherEntitySelector.html';
    })();
    
    //获取ctx路径
    var ctxPath = (function(){
        var wwwPath=window.document.location.href;
        var pathName=window.document.location.pathname;
        var pos=wwwPath.indexOf(pathName);
        var localhostPath=wwwPath.substring(0,pos);
        var projectName = pathName.substring(0,pathName.substr(1).indexOf('/')+1);
        return localhostPath + projectName;
    })();
    
    //自适应屏幕
    function voucherEntitySelector_autoResize()
    {
        $("#voucherEntitySelector_grid_area").jqGrid('setGridWidth', $("#voucherEntitySelector_search_area").width());
    }

    var voucherEntityGrid = null;
    //加载用户列表
    function initSelectorvoucherEntityGrid()
    {
    	voucherEntityGrid = $("#voucherEntitySelector_grid_area").jqGrid({
	        url:ctxPath + "/voucherEntity/getPageInfo.do",//查询列表接口
	        datatype : "json",
	        colNames:['id','栏目编码','栏目名称'],
	        colModel:[
	              {name:'id', hidden:true},
	              {name:'code', index:'code', width:'10'},
	              {name:'name',index:'name', width:'20'}
	        ],
	        viewrecords : true,
	        pager: '#voucherEntitySelector_page_area'
	   });
    	
       return voucherEntityGrid;
    };
    
	/**
     * 这里定义的对象，不会污染命名空间
     * jqueryObj 传入绑定此插件的jquery对象
     * */
    var _openLayer = function(jqueryObj, settings) {
        
        //加载html内容
        $.get(htmlPath, {}, function(content){
            var voucherEntity_selector_layer = layer.open({
                title: "选择栏目",
                type: 1,
                content: content,
                area: ['700px', '500px'],
                offset: '0px',
                btn: ['确定', '关闭'],
                maxmin: true, //允许最大化
                resizing: function(layero){
                    voucherEntitySelector_autoResize();
                },
                full: function(layero){
                    voucherEntitySelector_autoResize();
                },
                min: function(layero){
                    voucherEntitySelector_autoResize();
                },
                restore: function(layero){
                    voucherEntitySelector_autoResize();
                },
                yes: function(index, layero){
                    var selectids = $('#voucherEntitySelector_grid_area').jqGrid('getGridParam','selarrrow');
                    var selectRows = [];
                    $.each(selectids,function(i,n){
                        var row = $('#voucherEntitySelector_grid_area').jqGrid('getRowData',n);
                        selectRows.push(row);
                    });
                    
                    if(typeof settings.callback === 'function') {
                        settings.callback(selectRows);
                    }
                    layer.close(voucherEntity_selector_layer);
                    
                },
                cancel: function(){
                    settings.cancel && settings.cancel();
                },
                btn2: function(index, layero){
                    settings.close && settings.close();
                },
                success: function(layero, index){
                	initSelectorvoucherEntityGrid();   
                	
                	 //重置查询条件
                    $("#voucherEntitySelector_gotoReset").on("click", function(){
                        $('#voucherEntitySelector_search_form').resetForm();
                    });
                	
                	//执行查询
                    $('#voucherEntitySelector_gotoSearch').on("click", function(){
                  	  
                  	    var postData = voucherEntityGrid.getGridParam("postData");
                        $.each(postData, function (k, v) { delete postData[k]; });
                        
                        $.extend(postData, {
                            'code' : $('#voucherEntitySelector_search_form input[name="code"]').val(),
                            'name' : $('#voucherEntitySelector_search_form input[name="name"]').val()
                        });
                        
                        voucherEntityGrid.setGridParam({ postData: postData, page: 1});
                        voucherEntityGrid.trigger("reloadGrid");
                      
                    });
                }
            });
            
        });
        
    };
    

    //插件入口
    $.fn.voucherEntitySelector = function(options){
    	
    	//this代表绑定此插件的jquery对象
        var jqueryObj = this;
        
        //这里配置此插件的默认参数
        var defaults = {
            multiple : false
        };
        
        //通过把defaults options的属性都复制给一个空对象，避免污染defaults对象
        var settings = $.extend({}, defaults, options);
    
        //this指的是用jQuery选中的元素
        jqueryObj.on('click',function(){
        	
        	if(settings.validateTree){
        		var treeObjTest = $.fn.zTree.getZTreeObj(settings.validateTree);
            	var sNodes = treeObjTest.getSelectedNodes(true);
            	
            	if(sNodes.length < 1){
            		layer.msg("！", {
    					icon : 1,
    					time : 1000
    				})
    				return
            	}
        	}
            
            if(typeof settings.beforeLoad === 'function') {
                if(settings.beforeLoad() == false) return;
            }
            
            //弹出界面
            _openLayer(jqueryObj, settings);
        });
    
    };

})(jQuery,window,layer,document);